package cn.mumzone.pos.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.mumzone.basic.base.BaseActivity;
import cn.mumzone.basic.dialog.AlertDialog;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.R;
import cn.mumzone.pos.dialog.PosDialog;
import cn.mumzone.pos.event.ChangeShiftEvent;
import cn.mumzone.pos.model.ScheduleDetailEntity;
import cn.mumzone.pos.model.ScheduleEmployeeEntity;
import cn.mumzone.pos.model.ServerEntity;
import cn.mumzone.pos.presenter.SchedulePresenter;
import cn.mumzone.pos.presenter.contract.ScheduleContract;
import cn.mumzone.pos.ui.adapter.ADA_ScheduleEmployee;
import cn.mumzone.pos.ui.widgets.others.FullyGridLayoutManager;
import cn.mumzone.pos.ui.widgets.schedule.ScheduleView;

/**
 * Created by qiuzhenhuan on 2019/7/14.
 */

public class ACT_ChangeShift extends BaseActivity<SchedulePresenter> implements ScheduleContract.View {
    private static final String KEY_FROM_SCHEDULE = "fromSchedule";
    private static final String KEY_FROM_SCHEDULE_DAY = "fromScheduleDay";
    private static final String KEY_FROM_EMPLOYEE = "fromEmployee";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.sv_schedule)
    ScheduleView scheduleView;
    @BindView(R.id.ll_shift_list)
    LinearLayout llShiftList;
    @BindView(R.id.sv_shift_list)
    NestedScrollView scShiftList;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;

    private ScheduleEmployeeEntity mSelectedEmployee;

    private AlertDialog mChangeShiftDialog;

    private ScheduleDetailEntity fromSchedule;
    private String fromScheduleDay;
    private String fromScheduleShift;
    private ScheduleEmployeeEntity fromEmployee;
    private ScheduleDetailEntity mSelectedSchedule;

    @Override
    protected void getBundleExtras(Bundle extras) {
        fromSchedule = (ScheduleDetailEntity) extras.getSerializable(KEY_FROM_SCHEDULE);
        fromScheduleDay = extras.getString(KEY_FROM_SCHEDULE_DAY);
        fromEmployee = (ScheduleEmployeeEntity) extras.getSerializable(KEY_FROM_EMPLOYEE);
        fromScheduleShift = String.format("%s - %s",
                AppDateUtil.getDateToString(Long.valueOf(fromSchedule.getScheduleStartTime()), "HH:mm"),
                AppDateUtil.getDateToString(Long.valueOf(fromSchedule.getScheduleEndTime()), "HH:mm"));
    }

    @Override
    protected void initEventAndData() {

        tvTitle.setText(String.format("换班-%s", fromEmployee.getEmployeeName()));

        scheduleView.setOnDateItemClickListener(new ScheduleView.OnDateItemClickListener() {
            @Override
            public void onDateItemClicked(String date) {
                mPresenter.getScheduleDetail(date);
            }
        });
        scheduleView.clearCheck();
        scheduleView.setNotScheduleEnable(false);

        mPresenter.getAllScheduleInfo();
        showScheduleDetail(null);
        initChangeShiftDialog();
    }

    @Override
    public void showScheduleStatus(List<String> scheduleEntities) {
        scheduleView.setScheduleStatus(scheduleEntities);
    }

    @Override
    public void showScheduleDetail(List<ScheduleDetailEntity> scheduleDetailEntityList) {
        llShiftList.removeAllViews();
        if (!ListUtil.isListEmpty(scheduleDetailEntityList)) {
            scShiftList.setVisibility(View.VISIBLE);
            tvEmpty.setVisibility(View.GONE);
            for (final ScheduleDetailEntity entity : scheduleDetailEntityList) {
                View view = LayoutInflater.from(mContext).inflate(R.layout.view_shift, null);
                TextView tvShiftTime = view.findViewById(R.id.tv_shift_time);
                tvShiftTime.setText(String.format("%s - %s", AppDateUtil.getDateToString(Long.valueOf(entity.getScheduleStartTime()), "HH:mm"),
                        AppDateUtil.getDateToString(Long.valueOf(entity.getScheduleEndTime()), "HH:mm")));
                RecyclerView rvEmployee = view.findViewById(R.id.rv_employee);
                rvEmployee.setLayoutManager(new FullyGridLayoutManager(mContext, 3));
                ADA_ScheduleEmployee adapter = new ADA_ScheduleEmployee(mContext);
                adapter.setDataList(entity.getScheduleWorkList());
                rvEmployee.setAdapter(adapter);
                adapter.setHasAddButton(false);
                adapter.setOnItemClickListener(new ADA_ScheduleEmployee.OnItemClickListener() {
                    @Override
                    public void onItemClick(ScheduleEmployeeEntity employeeEntity, int position) {
                        mSelectedSchedule = entity;
                        if (fromEmployee.getEmployeeId().equals(employeeEntity.getEmployeeId())) {
                            PosToast.INSTANCE.makeShortToast(mContext, "不能选择同一个技师");
                            return;
                        }
                        if (isEmployeeInSchedule(fromEmployee, mSelectedSchedule)
                                || isEmployeeInSchedule(employeeEntity, fromSchedule)) {
                            PosToast.INSTANCE.makeShortToast(mContext, "技师已在班次中");
                            return;
                        }
                        mSelectedEmployee = employeeEntity;
                        TextView tvToEmployeeName = mChangeShiftDialog.getView(R.id.tv_to_employee_name);
                        TextView tvToDay = mChangeShiftDialog.getView(R.id.tv_to_day);
                        TextView tvToShift = mChangeShiftDialog.getView(R.id.tv_to_employee_shift);
                        tvToDay.setText(AppDateUtil.getDateToString(AppDateUtil.getStringToDate(scheduleView.getSelectedDate(), "yyyy-MM-dd"), "MM月dd日"));
                        tvToEmployeeName.setText(mSelectedEmployee.getEmployeeName());
                        tvToShift.setText(String.format("%s - %s",
                                AppDateUtil.getDateToString(Long.valueOf(mSelectedSchedule.getScheduleStartTime()), "HH:mm"),
                                AppDateUtil.getDateToString(Long.valueOf(mSelectedSchedule.getScheduleEndTime()), "HH:mm")));
                        showDialog(mChangeShiftDialog);

                    }
                });
                llShiftList.addView(view);
            }
        } else {
            scShiftList.setVisibility(View.GONE);
            tvEmpty.setVisibility(View.VISIBLE);
        }
    }

    private boolean isEmployeeInSchedule(ScheduleEmployeeEntity employeeEntity, ScheduleDetailEntity scheduleDetailEntity) {
        for (ScheduleEmployeeEntity entity : scheduleDetailEntity.getScheduleWorkList()) {
            if (entity.getEmployeeId().equals(employeeEntity.getEmployeeId())) {
                return true;
            }
        }
        return false;
    }

    @OnClick({R.id.iv_back})
    void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void initChangeShiftDialog() {

        mChangeShiftDialog = new PosDialog.PosBuilder(mContext)
                .setRealDialogContent(R.layout.dialog_change_shift)
                .setDialogTitle("换班信息")
                .setWidthAndHeight(438, 295)
                .setOnSingleConfirmListener(new AlertDialog.Builder.OnSingleConfirmListener() {
                    @Override
                    public void onConfirmClicked(AlertDialog dialog) {
                        if (mPresenter.isNowSchedule(scheduleView.getSelectedDate(), mSelectedSchedule)) {
                            return;
                        }
                        mPresenter.doChangeShift(fromEmployee, mSelectedEmployee);
                    }
                })
                .create();

        TextView tvFromEmployeeName = mChangeShiftDialog.getView(R.id.tv_from_employee_name);
        TextView tvFromDay = mChangeShiftDialog.getView(R.id.tv_from_day);
        TextView tvFromShift = mChangeShiftDialog.getView(R.id.tv_from_employee_shift);
        tvFromEmployeeName.setText(fromEmployee.getEmployeeName());
        tvFromDay.setText(AppDateUtil.getDateToString(AppDateUtil.getStringToDate(fromScheduleDay, "yyyy-MM-dd"), "MM月dd日"));
        tvFromShift.setText(fromScheduleShift);
    }


    @Override
    public void showChangeShiftSuccess(String result) {
        RxBus.getDefault().post(new ChangeShiftEvent(fromScheduleDay));
        PosToast.INSTANCE.makeShortToast(mContext, result);
        finish();
    }

    @Override
    public void showAddPersonShiftSuccess(String result) {

    }

    @Override
    public void showRemovePersonShiftSuccess(String result) {

    }

    @Override
    public void showServerList(List<ServerEntity> servers) {

    }

    @Override
    public void showCheckDoChangeShiftSuccess() {

    }

    @Override
    public void showError(String msg) {
        PosToast.INSTANCE.makeShortToast(mContext, msg);
    }


    public static void startAc(Context context, String scheduleDay, ScheduleDetailEntity fromSchedule, ScheduleEmployeeEntity employee) {
        Intent intent = new Intent(context, ACT_ChangeShift.class);
        intent.putExtra(KEY_FROM_SCHEDULE_DAY, scheduleDay);
        intent.putExtra(KEY_FROM_SCHEDULE, fromSchedule);
        intent.putExtra(KEY_FROM_EMPLOYEE, employee);
        context.startActivity(intent);
    }


    @Override
    protected int getLayout() {
        return R.layout.fra_schedule;
    }
}
